// pages/Coupon/createCoupon/createCoupon.js
var dateTimePicker  = require("./../../../utils/dateTime")
import Https from "../../../utils/api"
const http = new Https
Page({

  /**
   * 页面的初始数据
   */
  data: {
    image:"",
    pink: {
      title: '',
      live_img: '',
      is_record: '',
      start_date: '',
      start_time: '',
      end_date: '',
      end_time: '',
      meeting_date:"",
      meeting_time:""
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
   
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    //var lastArray = obj1.dateTimeArray.pop();
    //var lastTime = obj1.dateTime.pop();

    this.setData({
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime
    });
  },

  formSubmit(e) {
    console.log(e)
    let value = e.detail.value;
    let time = this.data.pink;

    if(value.car_number == ""){
      wx.showToast({
        title: '车牌号必须',
        icon:"error"
      })
      return;
    }
    if(value.gather_site == ""){
      wx.showToast({
        title: '集合地址必须',
        icon:"error"
      })
      return;
    }
    if(value.houses_name == ""){
      wx.showToast({
        title: '团购标题必须',
        icon:"error"
      })
      return;
    }
    if(value.site_detail == ""){
      wx.showToast({
        title: '车牌号必须',
        icon:"error"
      })
      return;
    }
    if(time.start_date == ""){
      wx.showToast({
        title: '时间必须',
        icon:"error"
      })
      return;
    }
    if(time.start_time == ""){
      wx.showToast({
        title: '时间必须',
        icon:"error"
      })
      return;
    }
    if(time.end_date == ""){
      wx.showToast({
        title: '时间必须',
        icon:"error"
      })
      return;
    }
    if(time.end_time == ""){
      wx.showToast({
        title: '时间必须',
        icon:"error"
      })
      return;
    }
    let data = {
      image:this.data.image,
      houses_name:value.houses_name,
      group_desc:value.group_desc,
      site_province:wx.getStorageSync('province'),
      site_detail:value.site_detail,
      group_number:value.group_number,
      car_number:value.car_number,
      gather_site:value.gather_site,
      start_time:time.start_date +" "+time.start_time,
      end_time:time.end_date +" "+ time.end_time,
      meeting_time:time.meeting_date +" "+ time.meeting_time
    }
    http.request("pink/creation",{data},"POST").then(res=>{
      console.log(res)
      if(res.code == 200){
        wx.showToast({
          title: res.message,
          icon:"none"
        })
        setTimeout(function(){
          wx.navigateTo({
            url: '/pages/Coupon/Coupon',
          })
        },2000)
      }else{
        wx.showToast({
          title: res.message,
          icon:"error"
        })
      }
    })
  },

  uploadFile(e){
    let that = this
    console.log(1)
    wx.chooseImage({
      success (res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: 'http://www.sx.com/api/upfile', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'user': 'test'
          },
          success (res){
            let data = JSON.parse(res.data)
            if(data.code == 200){
              that.setData({image:data.data})
            }
            //do something
          }
        })
      }
    })
  },

  bindMeetingDate: function(e) {
    let pink = this.data.pink;
    pink.meeting_date = e.detail.value
    this.setData({pink});
  },
  bindMeetingTime: function(e) {
    let pink = this.data.pink;
    pink.meeting_time = e.detail.value
    this.setData({pink});
  },

  bindStartDateChange: function(e) {
    let pink = this.data.pink;
    pink.start_date = e.detail.value
    this.setData({pink});
  },
  bindEndDateChange: function(e) {
    let pink = this.data.pink;
    pink.end_date = e.detail.value
    this.setData({pink});
  },
  bindStartTimeChange: function(e) {
    let pink = this.data.pink;
    pink.start_time = e.detail.value
    this.setData({pink});
  },
  bindEndTimeChange: function(e) {
    let pink = this.data.pink;
    pink.end_time = e.detail.value
    this.setData({pink});
  },

  changeDateTime1(e) {
    this.setData({
      dateTime1: e.detail.value
    });
  },
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1,
      dateArr = this.data.dateTimeArray1;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr

    });
    console.log(dateArr)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})